package com.company.management.controller.performance;


import com.company.framework.pojo.CommonResult;
import com.company.framework.pojo.PageResult;
import com.company.management.domain.req.PerformanceEvaluationsReq;
import com.company.management.domain.req.PerformancePageReq;
import com.company.management.domain.req.PerformanceResultPageReq;
import com.company.management.domain.req.PerformanceResultsReq;
import com.company.management.domain.rsp.PerformanceEvalResultsResp;
import com.company.management.domain.rsp.PerformanceEvaluationsResp;
import com.company.management.domain.rsp.PerformanceResultsResp;
import com.company.management.domain.rsp.UserTreeResp;
import com.company.management.dto.UserDto;
import com.company.management.service.PerformanceEvaluationUsersService;
import com.company.management.service.PerformanceEvaluationsService;
import com.company.management.service.PerformanceResultsService;
import com.company.management.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.company.framework.pojo.CommonResult.error;
import static com.company.framework.pojo.CommonResult.success;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author robet.li
 * @since 2025-03-02
 */

@Api(tags = "绩效考核服务")
@RestController
@RequestMapping("/api/Performance")
@Slf4j
@CrossOrigin(origins = "*")
public class PerformanceEvaluationsController {
    @Autowired
    private PerformanceEvaluationsService performanceevaluationsService;

    @Autowired
    private PerformanceResultsService performanceresultsService;

    @Autowired
    private PerformanceEvaluationUsersService performanceEvaluationUsersService;


    @ApiOperation("创建绩效考核")
    @PostMapping("/createPerformance")
    public CommonResult<Integer> createPerformance(HttpServletRequest request,@RequestBody PerformanceEvaluationsReq req){
        log.info("创建绩效考核");
        if (performanceevaluationsService.detailByEvaluationTitle(req.getEvaluationTitle())!=null){
            return error(0,"该考核主题已存在");
        }
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        Integer res = performanceevaluationsService.saveResult(req);
        return res>0?success(200):error(0,"创建失败");
    }
    @ApiOperation("删除绩效考核")
    @PostMapping("/deletePerformance")
    public CommonResult<Integer> deletePerformance(HttpServletRequest request,@RequestBody PerformanceEvaluationsReq req){
        log.info("创建绩效考核");
        if (performanceevaluationsService.detailByEvaluationTitle(req.getEvaluationTitle())!=null){
            return error(0,"该考核主题已存在");
        }
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        Integer res = performanceevaluationsService.deletePerformance(req);
        return res>0?success(200):error(0,"创建失败");
    }
    @ApiOperation("查询绩效考核")
    @PostMapping("/getPerformanceList")
    public CommonResult<PageResult<PerformanceEvaluationsResp>> getPerformanceList(HttpServletRequest request,@RequestBody PerformancePageReq req){
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        req.setIsAdmin(user.getIsAdmin());
        return success(performanceevaluationsService.getPerformanceList(req));
    }

    @ApiOperation("创建绩效考核结果")
    @PostMapping("/PerformanceResults")
    public CommonResult<Integer> performanceResults(HttpServletRequest request,@RequestBody PerformanceResultsReq req){
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        Integer res = performanceresultsService.saveResult(req);
        return res>0?success(200):error(0,"考核失败");
    }

    @ApiOperation("查询绩效考核和绩效考核结果")
    @PostMapping("/getPerformanceResults")
    public CommonResult<PageResult<PerformanceEvalResultsResp>> getPerformanceResults(HttpServletRequest request,@RequestBody PerformanceResultPageReq req) {
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        return success(performanceresultsService.getPerformanceResults(req));
    }
    @ApiOperation("查询绩效考核内容")
    @GetMapping("/detail/{id}")
    public CommonResult<PerformanceEvaluationsResp> performanceDetail(HttpServletRequest request,@PathVariable String id){
        PerformanceEvaluationsReq req = new PerformanceEvaluationsReq();
        if (id != null && !"".equals(id) && !"undefined".equals(id) ){
            req.setEvaluationId(Integer.parseInt(id));
        }
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        return success(performanceevaluationsService.detail(req));
    }

    @ApiOperation("查询创建绩效考核结果内容")
    @GetMapping("/performanceResults/detail/{id}")
    public CommonResult<PerformanceResultsResp> performanceResults(@PathVariable String id){
        return success(performanceresultsService.detail(id));
    }
    @ApiOperation("查询创建绩效考核结果内容")
    @GetMapping("/user/tree/{id}")
    public CommonResult<List<UserTreeResp>> getUserTree(HttpServletRequest request,@PathVariable String id) {
        PerformanceEvaluationsReq req = new PerformanceEvaluationsReq();
        if (id != null && !"".equals(id) && !"undefined".equals(id) ){
            req.setEvaluationId(Integer.parseInt(id));
        }
        UserDto user = performanceEvaluationUsersService.getUserId(request);
        if (user==null){
            return error(0,"用戶信息为空，请重新登录！");
        }
        req.setUserId(user.getUserId());
        return success(performanceEvaluationUsersService.getUserList(req));
    }
}
